﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FreeRecorderExplorer.Properties;

namespace FreeRecorderExplorer
{
    /// <summary>
    /// Interaction logic for ConnectionWindow.xaml
    /// </summary>
    public partial class ConnectionWindow : Window
    {
        public ConnectionWindow()
        {
            InitializeComponent();

            IPAddress [] addrs = Dns.GetHostAddresses(Dns.GetHostName());
            cbInterfaces.DataContext = addrs;
            txtPort.Text = Settings.Default.Port.ToString();
            if (Settings.Default.IPAddress != "")
            {
                try 
                {
                    cbInterfaces.SelectedItem = addrs.First(a => a.ToString() == Settings.Default.IPAddress);		
                }
                catch (Exception)
                {}
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Settings.Default.Port = ushort.Parse(txtPort.Text);
                if (cbInterfaces.SelectedItem != null)
                {
                    Settings.Default.IPAddress = ((IPAddress)cbInterfaces.SelectedItem).ToString();
                }
                Settings.Default.Save();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                return;
            }
            MessageBox.Show("Changes will be used after restart");
            DialogResult = true;
            Close();
        }
    }
}
